/**
 * 新增方法
 *
 * @param fields
 */
import {form, TableListItem} from "./data";
import {message} from "antd";
import React, {useRef, useState} from "react";
import {
  ActionType,
  PageContainer,
  ProColumns,
  ProTable
} from "@ant-design/pro-components";
import type {TableListPagination} from "@/pages/list/table-list/data";
import {queryDept} from './dept'

const handleAdd = async (fields: TableListItem) => {
  const hide = message.loading('正在添加');

  try {
    //TODO 新增接口调用
    hide();
    message.success('添加成功');
    return true;
  } catch (error) {
    hide();
    message.error('添加失败请重试！');
    return false;
  }
};
/**
 * 更新方法
 *
 * @param fields
 */

const handleUpdate = async (fields: form, currentRow?: TableListItem) => {
  const hide = message.loading('正在配置');

  try {
    //更新接口调用
    hide();
    message.success('配置成功');
    return true;
  } catch (error) {
    hide();
    message.error('配置失败请重试！');
    return false;
  }
};


/**
 * 删除方法
 *
 * @param selectedRows
 */

const handleRemove = async (selectedRows: TableListItem[]) => {
  const hide = message.loading('正在删除');
  if (!selectedRows) return true;

  try {
    //删除接口调用
    hide();
    message.success('删除成功，即将刷新');
    return true;
  } catch (error) {
    hide();
    message.error('删除失败，请重试');
    return false;
  }
};


const DeptList: React.FC = () => {

  const actionRef = useRef<ActionType>();
  const [currentRow, setCurrentRow] = useState<TableListItem>();
  const [selectedRowsState, setSelectedRows] = useState<TableListItem[]>([]);


  const columns: ProColumns<TableListItem>[] = [
    {
      title: '部门编码',
      dataIndex: 'deptCode',
    },
    {
      title: '部门名称',
      dataIndex: 'deptName',
    },
    {
      title: '组织路径',
      dataIndex: 'deptFullName',
    },
    {
      title: '更新时间',
      sorter: true,
      dataIndex: 'updateTime',
    },
  ];


  // @ts-ignore
  return (
    <PageContainer>
      <ProTable<TableListItem, TableListPagination>
        headerTitle="部门信息"
        actionRef={actionRef}
        rowKey="deptId"
        search={{
          labelWidth: 120,
        }}
        request={queryDept}
        columns={columns}
        rowSelection={{
          onChange: (_, selectedRows) => {
            setSelectedRows(selectedRows);
          },
        }}
      />
    </PageContainer>
  )
}

export default DeptList;
